package SA.teacher.answer;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import SA.teacher.*;
import SA.teacher.daily.*;
import SA.teacher.manage.ManageActivity1;
import SA.teacher.notice.NoticeActivity1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class AnswerActivity1 extends Activity {
	ListView list1;
	AnswerAdapter adapter1;
	ArrayList<AnswerData> arrData1;
	String id;
	String day = null;
	String rcvclassid;
	String tempanswer;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.answer1);

		Intent intent = getIntent();
		id = intent.getStringExtra("ID");

		Calendar today = Calendar.getInstance();

		int whatDay = today.get(Calendar.DAY_OF_WEEK);

		int tempday = whatDay % 7;
		switch (tempday) // 0~6占쏙옙占쏙옙 占쏙옙~占쌥울옙占싹뤄옙 표占쏙옙
		{
		case 0:
			day = "占쏙옙";
			break;
		case 1:
			day = "占쏙옙";
			break;
		case 2:
			day = "占쏙옙";
			break;
		case 3:
			day = "화";
			break;
		case 4:
			day = "占쏙옙";
			break;
		case 5:
			day = "占쏙옙";
			break;
		case 6:
			day = "占쏙옙";
			break;
		}// end switch

		setListData();
		adapter1 = new AnswerAdapter(this, arrData1, id);

		list1 = (ListView) findViewById(R.id.list);
		list1.setAdapter(adapter1);

		//////////////Button action////////////////////////////////////
		TextView noticeBtn = (TextView) findViewById(R.id.notice);
		noticeBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				noticeActivity();
			}
		});

		ImageView mainBtn = (ImageView) findViewById(R.id.mainBtn);
		mainBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mainActivity();
			}
		});

		ImageView manageBtn = (ImageView) findViewById(R.id.manageBtn);
		manageBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				manageActivity();
			}
		});

		ImageView dailyBtn = (ImageView) findViewById(R.id.dailyBtn);
		dailyBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dailyActivity();
			}
		});
	}

	private void setListData() {
		int i;
		String num = "";
		String title = "";
		String student = "";
		String yn ="";

		answernameparse ps = new answernameparse();


		ps.run();

		if(tempanswer.length()==2)
		{
			Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙 占싫듸옙");
			//���ш린 �덈뱾�닿���?
			mainActivity();
			/*
			new AlertDialog.Builder(AnswerActivity1.this)
			.setMessage("�꾩옱 �대떦�섎뒗 諛섏씠 �놁뒿�덈떎.")
			.setPositiveButton("�뺤씤", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					// TODO Auto-generated method stub
				}
			}).show();*/
		}
		else{
			Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙else 占싫듸옙");
			arrData1 = new ArrayList<AnswerData>();

			String[] idlist = tempanswer.split("-");

			Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙els1e 占싫듸옙");
			int tempnum = Integer.parseInt(idlist[0]);//占쏙옙占쏙옙占�占쏙옙트占쏙옙 占쏙옙환
			String[] noname = idlist[1].split(">");

			Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙else 2占싫듸옙");
			for (i = 0; i < tempnum; i++) {

				Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙els3e 占싫듸옙");
				String[] tempcon = noname[i].split(",");
				num = tempcon[0];
				title = tempcon[1];
				student = tempcon[2];
				yn = tempcon[3];

				Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙else 4占싫듸옙");
				arrData1.add(new AnswerData(num, title, student, yn));

				Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙el5se 占싫듸옙");
			}
		}
	}


	public class answernameparse implements Runnable {
		public void run() {

			// TODO Auto-generated method stub
			try {
				URL url = new URL("Http://220.67.128.8/answername.php?id="+ URLEncoder.encode(id, "EUC-KR"));

				HttpURLConnection conn = (HttpURLConnection) url.openConnection();

				InputStream is = null;
				ByteArrayOutputStream baos = null;
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					is = conn.getInputStream();
					baos = new ByteArrayOutputStream();
					byte[] byteBuffer = new byte[1024];
					byte[] byteData = null;
					int nLength = 0;
					while ((nLength = is.read(byteBuffer)) > 0) {
						baos.write(byteBuffer, 0, nLength);
					}
					byteData = baos.toByteArray();
					String response = new String(byteData, "euc-kr");
					if (response == null || response.equals("")) {
						throw new Exception("Response is NULL!");
					}
					tempanswer=response;

					Log.e("login 占쏙옙占쏙옙 : ",response);
				} else {
					Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙 占싫듸옙");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
			}
		}
	}

	private class classidparse implements Runnable {
		public void run() {	Log.e("login 占쏙옙占쏙옙 : ", id);
		Log.e("login 占쏙옙占쏙옙 : ", day);
		// TODO Auto-generated method stub
		try {
			URL url = new URL("Http://220.67.128.8/dayclassid.php?id="+ URLEncoder.encode(id, "EUC-KR") );
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			InputStream is = null;
			ByteArrayOutputStream baos = null;
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				is = conn.getInputStream();
				baos = new ByteArrayOutputStream();
				byte[] byteBuffer = new byte[1024];
				byte[] byteData = null;
				int nLength = 0;
				while ((nLength = is.read(byteBuffer)) > 0) {
					baos.write(byteBuffer, 0, nLength);
				}
				byteData = baos.toByteArray();
				String response = new String(byteData, "euc-k-r");
				rcvclassid = response;
				if (response == null || response.equals("")) {
					throw new Exception("Response is NULL!");
				}
			} else {
				Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙 占싫듸옙");
			}
		} catch (Exception e) {
		}
		}
	}


	//////////////Button �⑥닔��///////////////////////////////////
	void noticeActivity() {
		Intent intent = new Intent(this, NoticeActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void mainActivity() {
		Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void manageActivity() {
		classidparse cidps = new classidparse();
		cidps.run();
		final String[] classes = rcvclassid.split(",");
		if(classes.length-1!=0){
			new AlertDialog.Builder(AnswerActivity1.this)
			.setTitle("諛섏쓣 �좏깮�댁＜�몄슂.")
			.setItems(classes, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int which) {
					Intent intent = new Intent(AnswerActivity1.this, ManageActivity1.class);
					intent.putExtra("ID",id);
					intent.putExtra("classname",classes[which]);
					startActivity(intent);
				}
			}).show();
		}
		else{
			new AlertDialog.Builder(AnswerActivity1.this)
			.setMessage("�꾩옱 �대떦�섎뒗 諛섏씠 �놁뒿�덈떎.")
			.setPositiveButton("�뺤씤", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					// TODO Auto-generated method stub
				}
			}).show();
		}
	}
	void dailyActivity() {
		Intent intent = new Intent(this, DailyActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}

	//////////////�ㅻ줈媛�린 醫낅즺////////////////////////////////////
	public boolean onKeyDown(int keyCode, KeyEvent event){
		switch(keyCode){
		case KeyEvent.KEYCODE_BACK:
			String alertTitle = getResources().getString(R.string.app_name);
			String buttonMessage = getResources().getString(R.string.alert_msg_exit);
			String buttonYes = getResources().getString(R.string.button_yes);
			String buttonNo = getResources().getString(R.string.button_no);

			new AlertDialog.Builder(AnswerActivity1.this)
			.setTitle(alertTitle)
			.setMessage(buttonMessage)
			.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					moveTaskToBack(true);
					finish();
				}
			})
			.setNegativeButton(buttonNo, null)
			.show();
		}
		return true;
	}

}
